package com.taptech.doufu.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.util.TTLog;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.adapter.BaseListAdapter;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.bean.personalcenter.SweepFlowerBean;
import com.taptech.doufu.services.PersonalNoteService;
import com.taptech.doufu.services.NovelFlowerService;
import com.taptech.doufu.services.SweepFlowerService;
import com.taptech.doufu.ui.adapter.FlowerRankAapater;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.UIUtil;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.ui.view.PullToRefreshListView;
import com.taptech.doufu.ui.view.PullToRefreshListView.OnLoadAndRefreshListener;
import com.taptech.doufu.ui.view.PullToRefreshListView.OnScrollToBottomListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;

public class FlowerRankActivity extends DiaobaoBaseActivity implements HttpResponseListener
{

    private PullToRefreshListView fans;
    private BaseListAdapter adapter;
    private String reviewId, novelId;
    private String noteId;
    private String object_type;
    private int pageIndex;
    private String last = "";

    private LinearLayout personal_center_fans_tip;
    private boolean pageHasNext;
    private boolean isRequesting = false;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        reviewId = getIntent().getStringExtra(Constant.SWEEP_FLOWER_USER);
        novelId = getIntent().getStringExtra(Constant.NOVEL_FLOWER_USER);
        noteId = getIntent().getStringExtra(Constant.NOTE_ID);
        object_type = getIntent().getStringExtra(Constant.OBJECT_TYPE);
        TTLog.d("tag", "the note id ================" + noteId);
        setContentView(R.layout.personal_center_activity_fans);
        personal_center_fans_tip = (LinearLayout) findViewById(R.id.personal_center_fans_tip);
        TextView typeName = (TextView) findViewById(R.id.member_type_name);
        typeName.setText("送花使者");
        initView();
    }

    private void initView()
    {
        fans = (PullToRefreshListView) findViewById(R.id.personal_center_fans_list);
        fans.setLoadmoreable(true);
        fans.setRefreshable(false);
        fans.setOnLoadAndRefreshListener(new OnLoadAndRefreshListener()
        {

            @Override
            public void onRefresh()
            {

            }

            @Override
            public void loadMore()
            {
                //loadMore();
            }
        });
        fans.setOnScrollToBottomListener(new OnScrollToBottomListener()
        {

            @Override
            public void OnScrollToBottom()
            {

                if (reviewId != null && !reviewId.equalsIgnoreCase(""))
                {
                    SweepFlowerService.getInstance().loadFlowerRank(reviewId, pageIndex, Constant.PAGE_SIZE_20, last, FlowerRankActivity.this);
                }
                if (novelId != null && !novelId.equalsIgnoreCase(""))
                {
                    if (object_type != null)
                    {
                        NovelFlowerService.getInstance().loadFlowerRank(novelId, Constant.PAGE_SIZE_20, object_type, last, FlowerRankActivity.this);
                    } else
                    {
                        NovelFlowerService.getInstance().loadFlowerRank(novelId, Constant.PAGE_SIZE_20, Constant.OBJECT_TYPE_NOVEL, last, FlowerRankActivity.this);
                    }

                }
                loadMoreData();
            }
        });
        adapter = new FlowerRankAapater(this);
        fans.setAdapter(adapter);
        if (reviewId != null && !reviewId.equalsIgnoreCase(""))
        {
            SweepFlowerService.getInstance().loadFlowerRank(reviewId, 0, Constant.PAGE_SIZE_20, "", FlowerRankActivity.this);
        }
        if (novelId != null && !novelId.equalsIgnoreCase(""))
        {
            if (object_type != null)
            {
                NovelFlowerService.getInstance().loadFlowerRank(novelId, Constant.PAGE_SIZE_20, object_type, last, FlowerRankActivity.this);
            } else
            {
                NovelFlowerService.getInstance().loadFlowerRank(novelId, Constant.PAGE_SIZE_20, Constant.OBJECT_TYPE_NOVEL, last, FlowerRankActivity.this);
            }
        }
        TTLog.d("tag", "the note id ================22222222" + noteId);
        if (noteId != null && !noteId.equalsIgnoreCase(""))
        {
            PersonalNoteService.getInstance().loadFlowerList(FlowerRankActivity.this, noteId, object_type, last, String.valueOf(Constant.PAGE_SIZE_20));
            TTLog.d("tag", "the note id ================3333333333" + noteId);
        }
    }

    private synchronized void loadMoreData()
    {
        if (isRequesting == false && noteId != null && !noteId.equalsIgnoreCase(""))
        {
            isRequesting = true;
            PersonalNoteService.getInstance().loadFlowerList(FlowerRankActivity.this, noteId, object_type, last, String.valueOf(Constant.PAGE_SIZE_20));
            TTLog.d("tag", "the note id ================3333333333" + noteId);
        }
    }

    @Override
    public void handleResponse(int handleType, HttpResponseObject response)
    {
        try
        {
            switch (handleType)
            {
                case SweepFlowerService.HANDLE_TYPE_LOAD_FLOWER_RANK:
                    final JSONObject jsonObject = (JSONObject) response.getData();
                    //				pageIndex = response.getPageIndex();
                    if (response.getStatus() == 0)
                    {
                        pageHasNext = jsonObject.getBoolean("has_next");
                        if (pageIndex == 0)
                        {
                            fans.onRefreshComplete();
                            getMainDate(jsonObject, pageIndex);
                        } else
                        {
                            getMainDate(jsonObject, pageIndex);
                        }
                        //					oldPage = pageIndex;
                        pageIndex = pageIndex + 1;
                        last = jsonObject.getString("last");
                    } else
                    {
                        UIUtil.toastMessage(this, Constant.loadingFail);
                    }
                    break;
                case NovelFlowerService.HANDLE_TYPE_LOAD_FLOWER_RANK:
                    final JSONObject novelObj = (JSONObject) response.getData();
                    //				pageIndex = response.getPageIndex();
                    if (response.getStatus() == 0)
                    {
                        pageHasNext = novelObj.getBoolean("has_next");
                        if (pageIndex == 0)
                        {
                            fans.onRefreshComplete();
                            getMainDate(novelObj, pageIndex);
                        } else
                        {
                            getMainDate(novelObj, pageIndex);
                        }
                        //					oldPage = pageIndex;
                        pageIndex = pageIndex + 1;
                        last = novelObj.getString("last");
                    } else
                    {
                        UIUtil.toastMessage(this, Constant.loadingFail);
                    }
                    break;
                case PersonalNoteService.HANDLE_TYPE_LOAD_FLOWER_LIST:
                    final JSONObject noteObj = (JSONObject) response.getData();
                    //				pageIndex = response.getPageIndex();
                    if (response.getStatus() == 0)
                    {
                        pageHasNext = noteObj.getBoolean("has_next");
                        if (pageIndex == 0)
                        {
                            fans.onRefreshComplete();
                            getMainDate(noteObj, pageIndex);
                        } else
                        {
                            getMainDate(noteObj, pageIndex);
                        }
                        //					oldPage = pageIndex;
                        pageIndex = pageIndex + 1;
                        last = noteObj.getString("last");
                    } else
                    {
                        UIUtil.toastMessage(this, Constant.loadingFail);
                    }
                    break;
                default:
                    break;
            }


        } catch (Exception e)
        {
            e.printStackTrace();
        } finally
        {
            isRequesting = false;
        }
    }

    @SuppressWarnings("unchecked")
    private void getMainDate(JSONObject jsonObject, int page)
    {
        //		List<SweepFlowerBean> mainList=null;
        LinkedList<Object> linkedList = new LinkedList<Object>();
        try
        {
            if (jsonObject.get("list") instanceof JSONArray)
            {
                //				mainList = new ArrayList<SweepFlowerBean>();
                JSONArray mainArray = jsonObject.getJSONArray("list");
                //				SweepFlowerBean contentBean = null;
                //				for (int i = 0; i < mainArray.length(); i++) {
                //					contentBean = new SweepFlowerBean();
                //					if (mainArray.get(i) instanceof JSONObject) {
                //						contentBean.setJson(mainArray.getJSONObject(i));
                //					}
                //					mainList.add(contentBean);
                //				}
                linkedList = DiaobaoUtil.jsonArray2BeanList(SweepFlowerBean.class, mainArray);
                adapter.setSuperDatas(linkedList);
            }
        } catch (JSONException e)
        {
            e.printStackTrace();
        }
        //		if (mainList.size() == 0 && mainList.size() != Constant.PAGE_SIZE_5) {
        //			personal_center_fans_tip.setVisibility(View.VISIBLE);
        //			fans.setVisibility(View.GONE);
        //		}
        if (linkedList.size() == 0)
        {
            personal_center_fans_tip.setVisibility(View.VISIBLE);
            fans.setVisibility(View.GONE);
        }
        fans.requestLayout();
        fans.setEnabled(false);
        //		if(page==-1){
        //			adapter.setDataSource(mainList);
        //		}else if(page== 0) {
        //			adapter.setDataSource(mainList);
        //			fans.setFull(false);
        //		} else {
        //			adapter.setSuperDatas(mainList);
        //		}
        if (!pageHasNext)
        {//(mainList!=null&&mainList.size()<Constant.SEPARATE_RETURN_DATA_NUMBER))) {
            fans.setFull(true);
            fans.getFootView().setVisibility(View.VISIBLE);
            fans.loadMoreComplete();
        }
    }

    public void back(View view)
    {
        onBackPressed();
    }
}
